package com.lvmama.pet.sync.message;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.lvmama.comm.bee.service.sync.SyncBaseNewService;
import com.lvmama.comm.jms.Message;
import com.lvmama.comm.jms.MessageProcesser;
import com.lvmama.comm.sync.pojo.SyncTrigger;
import com.lvmama.comm.vo.ConstantJMS;

/**
 * Sync trigger消息处理
 * 
 * @author pengfei
 * 
 */
public class GeneralSyncTriggerProcesser implements MessageProcesser {

	static Log LOG = LogFactory.getLog(GeneralSyncTriggerProcesser.class);
	
	private SyncBaseNewService syncBaseNewService;

	public void process(Message message) {
		
		LOG.info("new syncTrigger2.0 :"+message);

		Long pkValue = message.getObjectId();
		//主键为：任意ID，无实际意义，附加信息为JSON格式
		if (ConstantJMS.EVENT_TYPE.GENERAL_SYNC_TRIGGER_NEW.name().equals(message.getEventType())) {
			try {
				String addition = message.getAddition();
				Map<String, Object> params = new HashMap<String, Object>();
				LOG.info("receive additon:"+addition);
				JSONObject object = JSONObject.fromObject(addition);
		        Iterator<String> it = object.keys();
		        while(it.hasNext()) {
		        	String key = it.next();
		        	String value = object.getString(key);
		        	if(StringUtils.isNotEmpty(value)) {
		        		params.put(key, value);
		        	}
		        }

		        SyncTrigger syncTrigger = new SyncTrigger();
		        
		        if(params.get("pkValue")!=null){
		        	syncTrigger.setPkValue(params.get("pkValue").toString());
		        }
		        if(params.get("userName")!=null){
		        	syncTrigger.setUserName(params.get("userName").toString());
		        }
		        if(params.get("tableName")!=null){
		        	syncTrigger.setTableName(params.get("tableName").toString());
		        }
		        if(params.get("triggerType")!=null){
		        	syncTrigger.setTriggerType(params.get("triggerType").toString());
		        }
		        if(params.get("dataSourceId")!=null){
		        	syncTrigger.setDataSourceId(params.get("dataSourceId").toString());
		        }
		        if(params.get("fieldOne")!=null){
		        	syncTrigger.setFieldOne(params.get("fieldOne").toString());
		        }
		        if(params.get("fieldTwo")!=null){
		        	syncTrigger.setFieldTwo(params.get("fieldTwo").toString());
		        }  
		        if(params.get("fieldThree")!=null){
		        	syncTrigger.setFieldThree(params.get("fieldThree").toString());
		        }
		        if(params.get("status")!=null){
		        	syncTrigger.setStatus(params.get("status").toString());
		        }
		        
		        syncBaseNewService.saveSyncTrigger(syncTrigger);
				
			}catch (Exception e) {
				LOG.error(e.getMessage(),e);
			}
		}
	}

	public SyncBaseNewService getSyncBaseNewService() {
		return syncBaseNewService;
	}

	public void setSyncBaseNewService(SyncBaseNewService syncBaseNewService) {
		this.syncBaseNewService = syncBaseNewService;
	}

	
	
	
}
